<template>
  <div class="headlthtest-wrap">
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane v-for="(item, index) in tabsList" :key="index" :name="item.key" :label="item.name">
        <div v-for="(step, stepindex) in redata[item.key]" :key="step.id" class="headlth-list">
          <div class="headlth-title" :class="colorType[index]">*{{ step.title }}</div>  
          <el-radio-group v-model="step.userAnswer">
              <el-radio v-for="radio in step.optionList" :key="radio.id" :label="radio.id" class="headlth-radio" @change="(e) => changeRadio(e, radio.score, item.key, stepindex)">{{ radio.content }}</el-radio>
          </el-radio-group>
        </div>
        <el-col v-if="item.key != 'step_5'" :span="6" :offset="6">
          <el-button type="primary" @click="handleStep(item.key, index)">下一步</el-button>
        </el-col>
        <el-col v-if="item.key == 'step_5'" :span="6" :offset="6">
          <el-button type="primary" @click="handleSubmit(redata)">提交测评</el-button>
        </el-col>
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      title="测评结果"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      :append-to-body="true"
      :modal-append-to-body="true"
      class="dialogForm"
    >
      <div class="item_ul">
        <div class="item">
          <div class="title">日常生活活动能力</div>
          <div class="text">
            {{result.value1 | valueFormat}}
          </div>
        </div>
        <div class="item">
          <div class="title">精神状态</div>
          <div class="text">
            {{result.value2 | valueFormat}}
          </div>
        </div>
        <div class="item">
          <div class="title">感知与沟通能力</div>
          <div class="text">
            {{result.value3 | valueFormat}}
          </div>
        </div>
        <div class="item">
          <div class="title">社会参与能力</div>
          <div class="text">
            {{result.value4 | valueFormat}}
          </div>
        </div>
        <div class="item">
          <div class="title">综合评定</div>
          <div class="text">
            {{result.value5 | valueFormat}}
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    posthealthtestoption,
    getelderhealthtest
  } from "@/api/oldermanager";
  import { score } from "@/utils/score.js"
  export default {
    name: "Healthquestions",
    filters: {
			valueFormat(value) {
				if (value == 0) {
					return '能力完好'
				} else if (value == 1) {
					return '轻度受损'
				} else if (value == 2) {
					return '中度受损'
				} else if (value >= 3) {
					return '重度受损'
				} else {
					return '评估不准确'
				}
			}
		},
    props: {
      data: {
        type: Object,
        default() {
          return {};
        },
      },
      resid: {
        type: Number,
        default() {
          return 0
        }
      }
    },
    data() {
      return {
        activeName: 'step_1',
        tabsList: [
          {
            name: "基本状态评估",
            key: "step_1"
          },
          {
            name: "日常生活活动能力评估",
            key: "step_2"
          },
          {
            name: "精神状态评估",
            key: "step_3"
          },
          {
            name: "感知与沟通能力评估",
            key: "step_4"
          },
          {
            name: "社会参与能力评估",
            key: "step_5"
          },
        ],
        dialogVisible: false,
        redata: {},
        result: {},
        colorType: ['colorType0', 'colorType1', 'colorType2', 'colorType3', 'colorType4']
      }
    },
    watch: {
      data: {
        handler: function (newValue, oldValue) {
          this.redata = Object.assign(this.redata, newValue)
        },
        deep: true,
        immediate:true
      },
    },
    methods: {
      closeDialog() {
        this.dialogVisible = false;
      },
      handleStep(value, index) {
        let num = index + 2;
        this.activeName = "step_" + num;
      },
      changeRadio(value, score, key, index) {
        this.$set(this.redata[key][index], "userScore", score ? score : 0)
      },
      handleSubmit(data) {
        let testData = {
          test0: this.redata.step_1,
          test1: this.redata.step_2,
          test2: this.redata.step_3,
          test3: this.redata.step_4,
          test4: this.redata.step_5
        }
        let answer = [];
        this.redata.step_1.forEach(item => {
          answer.push(item.id + "-" + item.answer)
        })
        this.redata.step_2.forEach(item => {
          answer.push(item.id + "-" + item.answer)
        })
        this.redata.step_3.forEach(item => {
          answer.push(item.id + "-" + item.answer)
        })
        this.redata.step_4.forEach(item => {
          answer.push(item.id + "-" + item.answer)
        })
        this.redata.step_5.forEach(item => {
          answer.push(item.id + "-" + item.answer)
        })
        console.log(testData)
        score(testData).then(resp => {
          console.log('resp===', resp)
          this.dialogVisible = true;
          this.result = resp;
          let params = {
						daily_result: resp.value1,
						spirit_result: resp.value2,
						feel_result: resp.value3,
						s_result: resp.value4,
						test_result: resp.value5,
						res_id: this.resid,
          }
          getelderhealthtest("put", params).then(res => {
            if (res.code === 200) {
              this.$emit("handleQuestions", type)
							this.$message({
                type: "success",
                message: "提交成功!",
              });
						}
          })
          posthealthtestoption("post", {res_id: this.resid, answer: answer.join(",")}).then(res =>{
            if(res.code == 200) {
              // this.$emit("handleQuestions", type)
              // this.$message({
              //   type: "success",
              //   message: "提交成功!",
              // });
            }
          })
        })
        
      }
    },
    
  }
</script>

<style lang="scss" scoped>
  .colorType0 {
    color: #409EFF;
  }
  .colorType1 {
    color: #67C23A;
  }
  .colorType2 {
    color: #E6A23C;
  }
  .colorType3 {
    color: #F56C6C;
  }

  .colorType4 {
    color: #dc1668;
  }

  .headlth-list{
    width: 100%;
    padding: 20px 30px;
    box-sizing: border-box;
    .headlth-title{
      width: 100%;
      border-bottom: 1px solid #e9dfdf;
      padding-bottom: 10px;
      margin-bottom: 10px;
    }
    .headlth-radio{
      width: 100%;
      margin: 10px 0;
    }
  }

  .item_ul {
		padding: 0 24px;

		.item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-bottom: 32px;

			.title {
				font-size: 16px;
				color: #999999;
			}

			.text {
				color: #333;
				font-size: 16px;
			}
		}

	}
</style>
